package com.dd.ljzapplication.activity;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.ljzapplication.R;

import java.util.ArrayList;

import base.BaseUtilActivity;

/**
 * Created by LiJZ on 2016/12/16.
 */

public class QQBarActivity extends BaseUtilActivity {
	private AppBarLayout mAppBarLayout;
		private Toolbar mToolbar;
	private RecyclerView mRecyclerView;
	private ArrayList<String> mData;
	private CollapsingToolbarLayout collapsingToolbarLayout;

	@Override
	protected int setLayoutID() {
		return R.layout.activity_qq_bar;
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initActionBar() {

	}

	@Override
	protected void initView() {

		mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
		mToolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(mToolbar);
		mToolbar.setTitle("toolbar");
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toobar_layout);

		collapsingToolbarLayout.setTitle("");

		int totalScrollRange = mAppBarLayout.getTotalScrollRange();
		Log.i("####", "initView: " + totalScrollRange);

		mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
			@Override
			public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

				int totalScrollRange = mAppBarLayout.getTotalScrollRange();
				Log.d("####", "initView: " + totalScrollRange);
				if (verticalOffset < 0 && verticalOffset > -350) {

					double d = (-verticalOffset) * 1.0 / 300;
					int i = (int) (d * 100);
					mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
					mToolbar.getBackground().setAlpha(i);
				} else if (verticalOffset < -350) {
					mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
				}
			}
		});

		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

		mData = new ArrayList<>();
		for (int i = 0; i < 80; i++) {
			mData.add("DATA------>" + i);
		}
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.setAdapter(new MyAdapter());


	}

	@Override
	protected void initEvent() {

	}

	class MyAdapter extends RecyclerView.Adapter {

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = View.inflate(QQBarActivity.this, R.layout.item, null);
			MyHolder holder = new MyHolder(view);
			return holder;
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
			MyHolder myHolder = (MyHolder) holder;
			AlphaAnimation animation = new AlphaAnimation(((float) 0.0), ((float) 1.0));
			animation.setDuration(200);
			animation.setFillAfter(true);
			myHolder.itemView.setAnimation(animation);
			myHolder.mTextView.setText(mData.get(position));
		}

		@Override
		public int getItemCount() {
			return mData.size();
		}
	}

	class MyHolder extends RecyclerView.ViewHolder {
		TextView mTextView;

		public MyHolder(View itemView) {
			super(itemView);
			mTextView = (TextView) itemView.findViewById(R.id.tv);
		}
	}
}
